package edu.ccut.saturn.studio.logic.model.saturn;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.graphics.Image;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.search.IObserver;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher.SearchResult;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.freemaker.FreemarkerUtils;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.dialog.datadialog.SaturnDataDialog;
import edu.ccut.saturn.studio.export.createhtml.Parameters;
import edu.ccut.saturn.studio.logic.figure.SaturnFigure;
import edu.ccut.saturn.studio.logic.model.Shape;

public class SaturnDataNode extends Shape {

	private static final long serialVersionUID = -4767974901469020656L;

	public final static String KEY_NODE_DATA = "data";
	public final static String SHOW_NODE_DATA = Messages
			.getString("SaturnDataNode.data"); //$NON-NLS-1$

	public static final String KEY_DATA_NODE_NAME = "_property_name";

	public static final String KEY_DATA_NODE_DESCRITPION = "_property_description";

	public static final String KEY_DATA_NODE_SCOPE = "_property_scope";

	public static final String KEY_DATA_NODE_PREFIX = "_property_prefix";

	public static final String KEY_DATA_NODE_DATA_KEY = "_property_datakey";

	public static final String KEY_DATA_NODE_DATA_CLASSTYPE = "_property_classtype";

	public static final String KEY_DATA_NODE_DATA_NAMESPACE = "_property_namespace";

	public static final String KEY_DATA_NODE_DATA_SATURNDATATYPE = "_property_saturndatatype";

	public static final String SHOW_DATA_NODE_NAME = "name";

	public static final String SHOW_DATA_NODE_DESCRITPION = "description";

	public static final String SHOW_DATA_NODE_SCOPE = "data-scope";

	public static final String SHOW_DATA_NODE_PREFIX = "data-prefix";

	public static final String SHOW_DATA_NODE_DATA_KEY = "data-key";

	public static final String SHOW_DATA_NODE_DATA_CLASSTYPE = "classType";

	public static final String SHOW_DATA_NODE_DATATYPE = "data-type";

	public static final String SHOW_DATA_NODE_DATA_NAMESPACE = "namespace";

	public static final String SHOW_DATA_NODE_DATA_SATURNDATATYPE = "saturnDataType";

	public static final String SHOW_DATA_NODE_DATA_XMLKEY = "xmlkey";

	public static final String SHOW_DATA_NODE_DATA_ELEMENT = "elementkey";

	private String scope = "1";

	private String prefix = "";// else

	private String datakey = "";

	private String classtype = "edu.ccut.saturn.component.SaturnData";

	private String namespace = "";

	private String saturndatatype = "";

	private Image image = Activator.getImage(Images.DATA_ICON);

	private String xmlKey = "";

	private String elementKey = "";

	private static int number = 1;

	public SaturnDataNode() {
		super.setName(SHOW_NODE_DATA + number++);
	}

	public String getScope() {
		return scope;
	}

	public void setScope(String scope) {
		if (!this.scope.equals(scope)) {
			this.scope = scope;
			firePropertyChange(PROPERTERY_CHANGS, null, scope);
		}
	}

	public String getPrefix() {
		return prefix;
	}

	public void setPrefix(String prefix) {
		if (!this.prefix.equals(prefix)) {
			this.prefix = prefix;
			firePropertyChange(PROPERTERY_CHANGS, null, prefix);
		}

	}

	public String getDatakey() {
		return datakey;
	}

	public void setDatakey(String datakey) {
		if (!this.datakey.equals(datakey)) {
			this.datakey = datakey;
			firePropertyChange(PROPERTERY_CHANGS, null, datakey);
		}

	}

	public String getClasstype() {
		return classtype;
	}

	public void setClasstype(String classtype) {
		if (!this.classtype.equals(classtype)) {
			this.classtype = classtype;
			firePropertyChange(PROPERTERY_CHANGS, null, classtype);
		}
	}

	public String getNamespace() {
		return namespace;
	}

	public void setNamespace(String namespace) {
		if (!this.namespace.equals(namespace)) {
			this.namespace = namespace;
			firePropertyChange(PROPERTERY_CHANGS, null, classtype);
		}
	}

	public String getSaturndatatype() {
		return saturndatatype;
	}

	public void setSaturndatatype(String saturndatatype) {
		if (!this.saturndatatype.equals(saturndatatype)) {
			this.saturndatatype = saturndatatype;
			firePropertyChange(PROPERTERY_CHANGS, null, classtype);
		}
	}

	@Override
	public IFigure getFigure() {

		SaturnFigure saturnFigure = new SaturnFigure();
		saturnFigure.setIcon(image);
		saturnFigure.setName(super.getName());

		return saturnFigure;

	}

	@Override
	public Image getIcon() {
		return image;

	}

	@Override
	public Node doRefresh() {
		Node root = super.getRoot();

		Node dataNode = SaturnW3CUtil.createChildNode(root, KEY_NODE_DATA, null);
		SaturnW3CUtil.createChildNode(dataNode, SHOW_DATA_NODE_NAME, super
				.getName());

		SaturnW3CUtil
				.createChildNode(dataNode, SHOW_DATA_NODE_SCOPE, this.scope);

		SaturnW3CUtil.createChildNode(dataNode, SHOW_DATA_NODE_PREFIX,
				this.prefix);

		SaturnW3CUtil.createChildNode(dataNode, SHOW_DATA_NODE_DATA_KEY,
				this.datakey);

		SaturnW3CUtil.createChildNode(dataNode, SHOW_DATA_NODE_DATA_XMLKEY,
				this.xmlKey);

		SaturnW3CUtil.createChildNode(dataNode, SHOW_DATA_NODE_DATA_ELEMENT,
				this.elementKey);

		Node dataTypeNode = SaturnW3CUtil.createChildNode(dataNode,
				SHOW_DATA_NODE_DATATYPE, null);

		SaturnW3CUtil.createAttribute(dataTypeNode,
				SHOW_DATA_NODE_DATA_CLASSTYPE, this.classtype);

		SaturnW3CUtil.createAttribute(dataTypeNode,
				SHOW_DATA_NODE_DATA_NAMESPACE, this.namespace);

		SaturnW3CUtil.createAttribute(dataTypeNode,
				SHOW_DATA_NODE_DATA_SATURNDATATYPE, this.saturndatatype);

		super.createParemeters(dataNode);

		return dataNode;
	}

	@Override
	public Dialog getDialog() {
		return new SaturnDataDialog(super.getShell(), this);
	}

	@Override
	public Shape cloneShape(Point mousePoint, Shape shape) {
		SaturnDataNode saturnDataNode = new SaturnDataNode();
		SaturnDataNode saturn = (SaturnDataNode) shape;
		saturnDataNode.setName(saturn.getName());
		saturnDataNode.setDescription(saturn.getDescription());
		saturnDataNode.setClasstype(saturn.getClasstype());
		saturnDataNode.setDatakey(saturn.getDatakey());
		saturnDataNode.setNamespace(saturn.getNamespace());
		saturnDataNode.setLocation(mousePoint);
		saturnDataNode.setSaturndatatype(saturn.getSaturndatatype());
		saturnDataNode.setPrefix(saturn.getPrefix());
		saturnDataNode.setScope(saturn.getScope());
		saturnDataNode.setParameters(saturn.getParameters());
		saturnDataNode.setId(saturn.getId());
		saturnDataNode.setNextId(saturn.getNextId());
		return saturnDataNode;
	}

	@Override
	protected void setImage(Image image) {
		this.image = image;
	}

	public String getXmlKey() {
		return xmlKey;
	}

	public void setXmlKey(String xmlKey) {
		if (!this.xmlKey.equals(xmlKey)) {
			this.xmlKey = xmlKey;
			firePropertyChange(PROPERTERY_CHANGS, null, xmlKey);
		}
	}

	public String getElementKey() {
		return elementKey;
	}

	public void setElementKey(String elementKey) {
		if (!this.elementKey.equals(elementKey)) {
			this.elementKey = elementKey;
			firePropertyChange(PROPERTERY_CHANGS, null, elementKey);
		}
	}
	
	public List<SearchResult> keyMapInWorkspace(IObserver observer) {
		
		List<SearchResult> list = null;
		String[] suffixes = {"saturndata"};
		
		try {
			list = SaturnFileSearcher.getValueAndDocumentMapInWorkspace(suffixes , observer, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
	public List<SearchResult> keyMapInProject(IObserver observer) {
		List<SearchResult> list = null;
		
		try {
			list = SaturnFileSearcher.getValueAndDocumentMapInProject(super.findTheProject(), "saturndata",
					observer, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
}

	@Override
	public Parameters createNodeHtml(String filePath, String[] tempnames) {
		
		Map<String, String> infos = new HashMap<String, String>();
		String name = getName();
		Parameters para = new Parameters();
		
		infos.put("type", getClasstype());
	 	infos.put("keyValue", getDatakey());
	 	infos.put("receiveDataStart", getPrefix());
	 	infos.put("receiveDataWay",transferReceiveDataWay( getScope()));
		infos.put("description", getDescription());
		infos.put("name", getName());
	 	infos.put("id", getId());
	 	infos.put("dataKeyValue", getNamespace() + "://" + getSaturndatatype());
	 	infos.put("xmlKey", getXmlKey());
	 	infos.put("elementKey", getElementKey());
	 	

		para.setInfourl( name + "-data.html");
		para.setInfoname( name);
		para.setDescription(getDescription());
		para.setNodeType(Messages.getString("Action.DataNode")); //$NON-NLS-1$
		para.setImagename("data");
		
		String templatePath = "template/export/action/data";
		String Path = filePath + File.separator + name + "-data.html";
		FreemarkerUtils.writeTemplateToFile(templatePath, infos, Path);
		
		return para;
	}
	
	/**
	 * 数据节点中的接受数据方式的转换
	 * 
	 * @param receiveDataWay
	 * @return  String
	 */
	private static String transferReceiveDataWay(String receiveDataWay) {
		if(receiveDataWay.equals("2")) {
			return "request";
		} else {
			return "xml";
		} 	
	}
	
}
